package com.baitengsoft.patentapimonitor.api.controller;

import com.baitengsoft.patentapimonitor.api.entity.ScheduleJobLogEntity;
import com.baitengsoft.patentapimonitor.api.model.ScheduleJobViewModel;
import com.baitengsoft.patentapimonitor.api.model.param.ScheduleJobLogParam;
import com.baitengsoft.patentapimonitor.api.model.param.ScheduleJobParam;
import com.baitengsoft.patentapimonitor.api.service.ScheduleJobLogService;
import com.baitengsoft.patentapimonitor.api.service.ScheduleJobService;
import com.baitengsoft.patentapimonitor.common.utils.PageResult;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Admin on 2018/1/9.
 */

@RestController
@RequestMapping("api/v1/admin")
@Api(value = "后台-数据更新", description = "数据更新")
public class AdminScheduleJobController extends AbstractController {

    @Autowired
    private ScheduleJobService scheduleJobService;
    @Autowired
    private ScheduleJobLogService scheduleJobLogService;

    @ApiOperation(value = "获取数据列表")
    @RequestMapping(value = "/searchschedulejob", method = RequestMethod.POST)
    public Res searchScheduleJob(@RequestBody ScheduleJobParam param) {

        Map<String, Object> map = new HashMap<>();
        Boolean flag = false;
        try {
            PageResult<ScheduleJobViewModel> pageResult = scheduleJobService.searchScheduleJob(param);
            if (pageResult != null) {
                flag = true;
                map.put("data", pageResult);
            } else {
                map.put("msg", "暂无数据");
            }
        } catch (Exception e) {
            map.put("msg", "获取异常");
        }
        map.put("flag", flag);
        return Res.ok(map);
    }


    @ApiOperation(value = "获取报错详情列表")
    @RequestMapping(value = "/searchschedulejoblog", method = RequestMethod.POST)
    public Res searchScheduleJobLog(@RequestBody ScheduleJobLogParam param) {

        Map<String, Object> map = new HashMap<>();
        Boolean flag = false;
        try {
            PageResult<ScheduleJobLogEntity> pageResult = scheduleJobLogService.searchSJobLogByJobId(param);
            if (pageResult != null) {
                flag = true;
                map.put("data", pageResult);
            } else {
                map.put("msg", "暂无数据");
            }
        } catch (Exception e) {
            map.put("msg", "获取异常");
        }
        map.put("flag", flag);
        return Res.ok(map);
    }


    @ApiOperation(value = "获取详情")
    @RequestMapping(value = "/searchschedulejobbyid", method = RequestMethod.GET)
    public Res searchScheduleJobById(@RequestParam int id) {

        Map<String, Object> map = new HashMap<>();
        Boolean flag = false;
        try {
            ScheduleJobViewModel viewModel = scheduleJobService.searchSingleSchedule(id);
            if (viewModel != null) {
                flag = true;
                map.put("data", viewModel);
            } else {
                map.put("msg", "暂无数据");
            }
        } catch (Exception e) {
            map.put("msg", "获取异常");
        }
        map.put("flag", flag);
        return Res.ok(map);
    }


    @ApiOperation(value = "处理数据")
    @RequestMapping(value = "/processschedulejobbyids", method = RequestMethod.GET)
    public Res processScheduleJobByIds(@RequestParam String idStr) {

        Map<String, Object> map = new HashMap<>();
        Boolean flag = true;
        try {
            scheduleJobService.processScheduleJobByIds(idStr);
        } catch (Exception e) {
            flag = false;
            map.put("msg", "处理异常");
        }
        map.put("flag", flag);
        return Res.ok(map);
    }
}
